package com.dxd.onlineshop.mapper;

import com.dxd.onlineshop.pojo.Notification;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NotificationMapper {

    //获取所有用户(用于推送通知)
    @Select("SELECT DISTINCT id FROM users")
    List<Integer> getAllUserIds();

    //获取用户的所有通知
    @Select("SELECT * FROM notifications WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Notification> findAllByUserId(Integer userId);

    //插入通知
    @Insert("INSERT INTO notifications (user_id, content, type, is_read, created_at) " +
            "VALUES (#{userId}, #{content}, #{type}, #{isRead}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Notification notification);

    //获取用户所有的未读取通知
    @Select("SELECT * FROM notifications WHERE user_id = #{userId} AND is_read = false ORDER BY created_at DESC")
    List<Notification> findUnreadByUserId(Integer userId);

    //更新通知读取状态
    @Update("UPDATE notifications SET is_read = true WHERE id = #{id}")
    int markAsRead(Long id);

    //删除通知
    @Delete("DELETE FROM notifications WHERE id = #{id}")
    int deleteById(Long id);

    //获取通知(通过ID)
    @Select("SELECT * FROM notifications WHERE id = #{id}")
    Notification findById(Long id);
}